package com.ltkj.baseplatform.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ltkj.baseplatform.domain.ArtContent;
import com.ltkj.baseplatform.domain.vo.SerCommentDataVo;
import com.ltkj.baseplatform.domain.vo.SerTravelNotesVo;
import com.ltkj.baseplatform.mapper.ArtContentBusinessMapper;
import com.ltkj.baseplatform.mapper.SerTravelNotesMapper;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.system.api.domain.CommAttachment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.baseplatform.mapper.SerCommentDataMapper;
import com.ltkj.baseplatform.domain.SerCommentData;
import com.ltkj.baseplatform.service.ISerCommentDataService;
import org.springframework.util.StringUtils;

/**
 * 评论管理Service业务层处理
 * 
 * @author liupf
 * @date 2023-10-16
 */
@Service
public class SerCommentDataServiceImpl implements ISerCommentDataService 
{
    @Autowired
    private SerCommentDataMapper serCommentDataMapper;
    @Autowired
    private SerTravelNotesMapper serTravelNotesMapper;
    @Autowired
    private ArtContentBusinessMapper artContentBusinessMapper;

    /**
     * 查询评论管理
     * 
     * @param id 评论管理主键
     * @return 评论管理
     */
    @Override
    public SerCommentData selectSerCommentDataById(Long id)
    {
        return serCommentDataMapper.selectSerCommentDataById(id);
    }

    /**
     * 查询评论管理列表
     * 
     * @param serCommentData 评论管理
     * @return 评论管理
     */
    @Override
    public List<SerCommentDataVo> selectSerCommentDataList(SerCommentData serCommentData)
    {
        //这里只根据类型查询，不传入类型，就返回空集合
        if(!StringUtils.hasText(serCommentData.getTypeCode())){
            return Collections.EMPTY_LIST;
        }
        List<SerCommentDataVo> serCommentDataVos = serCommentDataMapper.selectSerCommentDataList(serCommentData);
        if(serCommentDataVos == null || serCommentDataVos.size() == 0){
            return serCommentDataVos;
        }
        Map<Long, String> groupMap = new HashMap<>();
        if("travel".equals(serCommentData.getTypeCode())){
            List<Long> travelIds = serCommentDataVos.stream().map(item -> item.getBizId()).collect(Collectors.toList());
            List<SerTravelNotesVo> serTravelNotesVos = serTravelNotesMapper.selectSerTravelNotesListByIds(travelIds);
            for (SerTravelNotesVo serTravelNotesVo : serTravelNotesVos) {
                groupMap.put(serTravelNotesVo.getId(),serTravelNotesVo.getTitle());
            }
        }else{
            List<Long> articleIds = serCommentDataVos.stream().map(item -> item.getBizId()).collect(Collectors.toList());
            List<ArtContent> artContents = artContentBusinessMapper.selectArtContentListByIds(articleIds.toArray(new Long[articleIds.size()]));
            for (ArtContent artContent : artContents) {
                groupMap.put(artContent.getId(),artContent.getTitle());
            }
        }
        for (SerCommentDataVo serCommentDataVo : serCommentDataVos) {
            serCommentDataVo.setTitle(groupMap.get(serCommentDataVo.getBizId()));
        }


        return serCommentDataVos;
    }

    /**
     * 新增评论管理
     * 
     * @param serCommentData 评论管理
     * @return 结果
     */
    @Override
    public int insertSerCommentData(SerCommentData serCommentData)
    {
        serCommentData.setCreateTime(DateUtils.getNowDate());
        serCommentData.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return serCommentDataMapper.insertSerCommentData(serCommentData);
    }

    /**
     * 修改评论管理
     * 
     * @param serCommentData 评论管理
     * @return 结果
     */
    @Override
    public int updateSerCommentData(SerCommentData serCommentData)
    {
        serCommentData.setUpdateTime(DateUtils.getNowDate());
        serCommentData.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return serCommentDataMapper.updateSerCommentData(serCommentData);
    }

    /**
     * 批量删除评论管理
     * 
     * @param ids 需要删除的评论管理主键
     * @return 结果
     */
    @Override
    public int deleteSerCommentDataByIds(Long[] ids)
    {
        return serCommentDataMapper.deleteSerCommentDataByIds(ids);
    }

    /**
     * 删除评论管理信息
     * 
     * @param id 评论管理主键
     * @return 结果
     */
    @Override
    public int deleteSerCommentDataById(Long id)
    {
        return serCommentDataMapper.deleteSerCommentDataById(id);
    }

    @Override
    public List<SerCommentDataVo> selectAppletCommentDataList(SerCommentData serCommentData) {
        return serCommentDataMapper.selectSerCommentDataList(serCommentData);
    }
}
